﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page, IPostBackEventHandler
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["KFC_ConnectionString"].ConnectionString);
    SqlCommand com = new SqlCommand();
    SqlDataAdapter da = new SqlDataAdapter();
    DataSet ds = new DataSet();
    Dictionary<Int32, Int32> cart = new Dictionary<int, int>();
    Dictionary<Int32, String> cartImage = new Dictionary<int, string>();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            try
            {
                Session["CategoryID"] = Int32.Parse(Request.QueryString["CategoryID"].ToString());
            }
            catch (Exception)
            {
                Session["CategoryID"] = 1;
            }

            if (Session["Cart"] == null)
            {
                Session["Cart"] = cart;
                Session["CartImage"] = cartImage;
            }

            loadFoodData();
            loadCategoryData();
            loadAllFood();
        }
    }

    private void loadAllFood()
    {
        com.Connection = con;

        com.CommandText = "SELECT * FROM Food";
        da.SelectCommand = com;
        DataSet allFoodDs = new DataSet();
        da.Fill(allFoodDs);

        Session["FoodListAll"] = allFoodDs.Tables[0].DefaultView;
    }

    private void loadCategoryData()
    {
        com.Connection = con;

        com.CommandText = "SELECT * FROM Category";
        da.SelectCommand = com;
        da.Fill(ds);

        Session["CategoryList"] = ds.Tables[0].DefaultView;
        DataList2.DataSource = Session["CategoryList"];
        DataList2.DataBind();
        ds.Clear();
        
    }

    private void loadFoodData()
    {
        com.Connection = con;

        com.CommandText = "SELECT * FROM Food WHERE CategoryID = " + Session["CategoryID"];
        da.SelectCommand = com;
        da.Fill(ds);

        Session["FoodList"] = ds.Tables[0].DefaultView;
        DataList1.DataSource = Session["FoodList"];
        DataList1.DataBind();
        ds.Clear();
        
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Label lb1 = new Label();
        lb1.Text = "123";
        billDiv.Controls.Add(lb1);
    }

    public void RaisePostBackEvent(string eventArgument) {
        
    }

    public string formatPrice(int price)
    {
        string stringPrice = price.ToString();
        string moneyUnit = " Đ";
        if (price < 1000)
        {
            return stringPrice + moneyUnit;
        }
        else if (price < 1000000)
        {
            return stringPrice.Substring(0, stringPrice.Length - 3) + "." + stringPrice.Substring(stringPrice.Length - 3) + moneyUnit;
        }
        else if (price < 1000000000)
        {
            return stringPrice.Substring(0, stringPrice.Length - 6) + "." + stringPrice.Substring(stringPrice.Length - 5, stringPrice.Length - 4) + "." + stringPrice.Substring(stringPrice.Length - 3) + moneyUnit;
        }
        else
        {
            return price + moneyUnit;
        }
    }

    public string formatPrice(string price)
    {
        if (String.IsNullOrEmpty(price))
        {
            return "";
        }
        else
        {
            return formatPrice(Int32.Parse(price));
        }
    }
}